package com.etc.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.etc.dao.UserMapper;
import com.etc.entity.User;
import com.etc.service.UserService;

@Controller
@SessionAttributes(value = "user")
public class UserXgController {

	@Autowired
	private UserService userService;

	@PostMapping("xgmmsx")
	@ResponseBody
	public String xgmmgn(@RequestParam(value = "newpsw") String newpsw, @RequestParam(value = "oldpsw") String oldpsw,
			@RequestParam(value = "newpsw2") String newpsw2, @RequestParam(value = "userId") int userId,
			HttpServletRequest request) {

		System.out.println(oldpsw);
		System.out.println(newpsw);
		System.out.println(newpsw2);
		System.out.println("userId:" + userId);

		if (newpsw.equals(newpsw2)) {
			User yonghu = userService.getUser(userId);
			System.out.println(yonghu);
			// 旧密码一致
			if (yonghu.getUserPwd().equals(oldpsw)) {
				yonghu.setUserPwd(newpsw);
				userService.update(yonghu);
				return "1";// 修改成功
			} else {
				return "0";
			}
		} else {
			return "2";// 两次密码不一致
		}
	}

}
